function Game (ele){
    //保存对象
 this.ele=document.querySelector(ele)


//1.搞地图
this.map=new Map(this.ele)
this.map.create()


 //2.搞食物
 this.food=new Food(this.ele)
 this.food.create()

 //3.搞蛇
 this.snake=new Snake(this.ele)
 this.snake.create()

}
 //水平移动
 Game.prototype.start = function(){
    setInterval(()=>{
        // 蛇动
        this.snake.move()
         // 判断吃食
         let snakeX = this.snake.snakeObj[0].x * 20
         let snakeY = this.snake.snakeObj[0].y * 20
         let foodX = this.food.foodX
         let foodY = this.food.foodY
         
         if (snakeX == foodX && snakeY == foodY) {
             // alert('真香')
 
             let last = this.snake.snakeObj[this.snake.snakeObj.length - 1]
 
             this.snake.snakeObj.push({
                 x: last.x, 
                 y : last.y, 
                 color: last.color, 
                 ele:null
             })
             this.snake.create() //重新绘制蛇
             // 食物动
             this.food.move()
         }

    }, 150)

    //监控键盘上下左右键，来控制改变蛇的方向
    document.addEventListener('keydown',(e)=>{
        e=e || window.event
        switch(e.keyCode){
            case 38:
                this.snake.direction='top'
                break;
            case 40:
                this.snake.direction='bottom'
                break;
                case 37:
                    this.snake.direction = 'left'
                    break;
                case 39:
                    this.snake.direction = 'right'
                    break;
                default:
                    break;

        }
    })


}

